<html><head><meta charset="utf-8"><title>40 打动面试官：线程池流程编排中的运用实战   -慕课专栏</title>
			<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
			<meta name="renderer" content="webkit">
			<meta property="qc:admins" content="77103107776157736375">
			<meta property="wb:webmaster" content="c4f857219bfae3cb">
			<meta http-equiv="Access-Control-Allow-Origin" content="*">
			<meta http-equiv="Cache-Control" content="no-transform ">
			<meta http-equiv="Cache-Control" content="no-siteapp">
			<link rel="apple-touch-icon" sizes="76x76" href="https://www.imooc.com/static/img/common/touch-icon-ipad.png">
			<link rel="apple-touch-icon" sizes="120x120" href="https://www.imooc.com/static/img/common/touch-icon-iphone-retina.png">
			<link rel="apple-touch-icon" sizes="152x152" href="https://www.imooc.com/static/img/common/touch-icon-ipad-retina.png">
			<link href="https://moco.imooc.com/captcha/style/captcha.min.css" rel="stylesheet">
			<link rel="stylesheet" href="https://www.imooc.com/static/moco/v1.0/dist/css/moco.min.css?t=201907021539" type="text/css">
			<link rel="stylesheet" href="https://www.imooc.com/static/lib/swiper/swiper-3.4.2.min.css?t=201907021539">
			<link rel="stylesheet" href="https://static.mukewang.com/static/css/??base.css,common/common-less.css?t=2.5,column/zhuanlanChapter-less.css?t=2.5,course/inc/course_tipoff-less.css?t=2.5?v=201907051055" type="text/css">
			<link charset="utf-8" rel="stylesheet" href="https://www.imooc.com/static/lib/ueditor/themes/imooc/css/ueditor.css?v=201907021539"><link rel="stylesheet" href="https://www.imooc.com/static/lib/baiduShare/api/css/share_style0_16.css?v=6aba13f0.css"></head>
			<body><div id="main">


<div class="main-con hide-menu">
    <!-- 左侧菜单 & 索引 -->
    
    <div class="right-content" style="padding-left: 0px;">
        <div class="container clearfix" id="top" style="width: 1134px; display: block;">
            
            
            <div class="center_con js-center_con l" style="width: 1134px;">
                <div class="article-con">
                                            <!-- 买过的阅读 -->
                        

                    
                    <div class="art-title" style="margin-top: 0px;">
                        40 打动面试官：线程池流程编排中的运用实战   
                    </div>
                    <div class="art-info clearfix">
                        
                        <span class="l">
                            更新时间：2019-11-22 09:39:59
                        </span>
                    </div>
                    <div class="art-top">
                                                <img src="https://img1.mukewang.com/5dd73c480001b35506400359.jpg" alt="">
                                                                        <div class="famous-word-box">
                            <img src="https://www.imooc.com/static/img/column/bg-l.png" alt="" class="bg1 bg">
                            <img src="https://www.imooc.com/static/img/column/bg-r.png" alt="" class="bg2 bg">
                            <div class="famous-word">没有智慧的头脑，就像没有蜡烛的灯笼。<p class="author">——托尔斯泰</p></div>
                        </div>
                                            </div>
                    <div class="art-content js-lookimg">
                        <div id="article_content">
                            <div class="cl-preview-section"><h2 id="引导语" style="font-size: 30px;">引导语</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">在线程池的面试中，面试官除了喜欢问 ThreadPoolExecutor 的底层源码外，还喜欢问你有没有在实际的工作中用过 ThreadPoolExecutor，我们在并发集合类的《场景集合：并发 List、Map 的应用场景》一文中说过一种简单的流程引擎，如果没有看过的同学，可以返回去看一下。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">本章就在流程引擎的基础上运用 ThreadPoolExecutor，使用线程池实现 SpringBean 的异步执行。</p>
</div><div class="cl-preview-section"><h2 id="流程引擎关键代码回顾" style="font-size: 30px;">1 流程引擎关键代码回顾</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">《场景集合：并发 List、Map 的应用场景》文中流程引擎执行 SpringBean 的核心代码为：</p>
</div><div class="cl-preview-section"><pre class="  language-java"><code class="prism  language-java">  <span class="token comment">// 批量执行 Spring Bean</span>
  <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">stageInvoke</span><span class="token punctuation">(</span>String flowName<span class="token punctuation">,</span> StageEnum stage<span class="token punctuation">,</span> FlowContent content<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    List<span class="token operator">&lt;</span>DomainAbilityBean<span class="token operator">&gt;</span>
        domainAbilitys <span class="token operator">=</span>
        FlowCenter<span class="token punctuation">.</span>flowMap<span class="token punctuation">.</span><span class="token function">getOrDefault</span><span class="token punctuation">(</span>flowName<span class="token punctuation">,</span> Maps<span class="token punctuation">.</span><span class="token function">newHashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>stage<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>CollectionUtils<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>domainAbilitys<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token string">"找不到该流程对应的领域行为"</span> <span class="token operator">+</span> flowName<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>DomainAbilityBean domainAbility <span class="token operator">:</span> domainAbilitys<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 执行 Spring Bean</span>
      domainAbility<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">入参是 flowName（流程名称）、stage（阶段）、content（上下文），其中 stage 中会执行很多 SpringBean，SpringBean 被执行的代码是 domainAbility.invoke(content)。</p>
</div><div class="cl-preview-section"><h2 id="异步执行-springbean" style="font-size: 30px;">2 异步执行 SpringBean</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">从上述代码中，我们可以看到所有的 SpringBean 都是串行执行的，效率较低，我们在实际业务中发现，有的 SpringBean 完全可以异步执行，这样既能完成业务请求，又能减少业务处理的 rt，对于这个需求，我们条件反射的有了两个想法：</p>
</div><div class="cl-preview-section"><ol>
<li style="font-size: 20px; line-height: 38px;">需要新开线程来异步执行 SpringBean，可以使用 Runable 或者 Callable；</li>
<li style="font-size: 20px; line-height: 38px;">业务请求量很大，我们不能每次来一个请求，就开一个线程，我们应该让线程池来管理异步执行的线程。</li>
</ol>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">于是我们决定使用线程池来完成这个需求。</p>
</div><div class="cl-preview-section"><h2 id="如何区分异步的-springbean" style="font-size: 30px;">3 如何区分异步的 SpringBean</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">我们的 SpringBean 都是实现 DomainAbilityBean 这个接口的，接口定义如下：</p>
</div><div class="cl-preview-section"><pre class="  language-java"><code class="prism  language-java"><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">DomainAbilityBean</span> <span class="token punctuation">{</span>

  <span class="token comment">/**
   * 领域行为的方法入口
   */</span>
  FlowContent <span class="token function">invoke</span><span class="token punctuation">(</span>FlowContent content<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">从接口定义上来看，没有预留任何地方来标识该 SpringBean 应该是同步执行还是异步执行，这时候我们可以采取注解的方式，我们新建一个注解，只要 SpringBean 上有该注解，表示该 SpringBean 应该异步执行，否则应该同步执行，新建的注解如下：</p>
</div><div class="cl-preview-section"><pre class="  language-java"><code class="prism  language-java"><span class="token comment">/**
* 异步 SpringBean 执行注解
 * SpringBean 需要异步执行的话，就打上该注解
*author  wenhe
*date 2019/10/7
*/</span>
<span class="token annotation punctuation">@Target</span><span class="token punctuation">(</span>ElementType<span class="token punctuation">.</span>TYPE<span class="token punctuation">)</span><span class="token comment">// 表示该注解应该打在类上</span>
<span class="token annotation punctuation">@Retention</span><span class="token punctuation">(</span>RetentionPolicy<span class="token punctuation">.</span>RUNTIME<span class="token punctuation">)</span>
<span class="token annotation punctuation">@Documented</span>
<span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">AsyncComponent</span> <span class="token punctuation">{</span>

<span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">接着我们新建了两个 SpringBean，并在其中一个 SpringBean 上打上异步的注解，并且打印出执行 SpringBean 的线程名称，如下图：<br>
<img class="" src="https://img.mukewang.com/5dd5fd640001cd7618201354.png" data-original="//img.mukewang.com/5dd5fd640001cd7618201354.png" alt="图片描述"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">图中实现了两个 SpringBean：BeanOne 和 BeanTwo，其中 BeanTwo 被打上了 AsyncComponent 注解，表明 BeanTwo 应该被异步执行，两个 SpringBean 都打印出执行的线程的名称。</p>
</div><div class="cl-preview-section"><h2 id="mock-流程引擎数据中心" style="font-size: 30px;">4 mock 流程引擎数据中心</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">《场景集合：并发 List、Map 的应用场景》一文中，我们说可以从数据库中加载出流程引擎需要的数据，此时我们 mock 一下，mock 的代码如下：</p>
</div><div class="cl-preview-section"><pre class="  language-java"><code class="prism  language-java"><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FlowCenter</span> <span class="token punctuation">{</span>

  <span class="token comment">/**
   * flowMap 是共享变量，方便访问
   */</span>
  <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> Map<span class="token operator">&lt;</span>String<span class="token punctuation">,</span> Map<span class="token operator">&lt;</span>StageEnum<span class="token punctuation">,</span> List<span class="token operator">&lt;</span>DomainAbilityBean<span class="token operator">&gt;&gt;&gt;</span> flowMap
      <span class="token operator">=</span> Maps<span class="token punctuation">.</span><span class="token function">newConcurrentMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">/**
   * PostConstruct 注解的意思就是
   * 在容器启动成功之后，初始化 flowMap
   */</span>
  <span class="token annotation punctuation">@PostConstruct</span>
  <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 初始化 flowMap mock</span>
    Map<span class="token operator">&lt;</span>StageEnum<span class="token punctuation">,</span> List<span class="token operator">&lt;</span>DomainAbilityBean<span class="token operator">&gt;&gt;</span> stageMap <span class="token operator">=</span> flowMap<span class="token punctuation">.</span><span class="token function">getOrDefault</span><span class="token punctuation">(</span><span class="token string">"flow1"</span><span class="token punctuation">,</span>Maps<span class="token punctuation">.</span><span class="token function">newConcurrentMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>StageEnum value <span class="token operator">:</span> StageEnum<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      List<span class="token operator">&lt;</span>DomainAbilityBean<span class="token operator">&gt;</span> domainAbilitys <span class="token operator">=</span> stageMap<span class="token punctuation">.</span><span class="token function">getOrDefault</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span> Lists<span class="token punctuation">.</span><span class="token function">newCopyOnWriteArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">if</span><span class="token punctuation">(</span>CollectionUtils<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>domainAbilitys<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        domainAbilitys<span class="token punctuation">.</span><span class="token function">addAll</span><span class="token punctuation">(</span>ImmutableList<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span>
            ApplicationContextHelper<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span>BeanOne<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
            ApplicationContextHelper<span class="token punctuation">.</span><span class="token function">getBean</span><span class="token punctuation">(</span>BeanTwo<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
        <span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        stageMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>value<span class="token punctuation">,</span>domainAbilitys<span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    flowMap<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"flow1"</span><span class="token punctuation">,</span>stageMap<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 打印出加载完成之后的数据结果</span>
    log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"init success,flowMap is {}"</span><span class="token punctuation">,</span> JSON<span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>flowMap<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><h2 id="新建线程池" style="font-size: 30px;">5 新建线程池</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">在以上操作完成之后，只剩下最后一步了，之前我们执行 SpringBean 时，是这行代码：domainAbility.invoke(content);</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">现在我们需要区分 SpringBean 是否是异步的，如果是异步的，丢到线程池中去执行，如果是同步的，仍然使用原来的方法进行执行，于是我们把这些逻辑封装到一个工具类中，工具类如下：</p>
</div><div class="cl-preview-section"><pre class="  language-java"><code class="prism  language-java"><span class="token comment">/**
 * 组件执行器
 * author  wenhe
 * date 2019/10/7
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ComponentExecutor</span> <span class="token punctuation">{</span>
	<span class="token comment">// 我们新建了一个线程池</span>
  <span class="token keyword">private</span> <span class="token keyword">static</span> ExecutorService executor <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ThreadPoolExecutor</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span>
                                                                   365L<span class="token punctuation">,</span> TimeUnit<span class="token punctuation">.</span>DAYS<span class="token punctuation">,</span>
                                                                   <span class="token keyword">new</span> <span class="token class-name">LinkedBlockingQueue</span><span class="token operator">&lt;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">// 如果 SpringBean 上有 AsyncComponent 注解，表示该 SpringBean 需要异步执行，就丢到线程池中去</span>
  <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span>DomainAbilityBean component<span class="token punctuation">,</span> FlowContent content<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 判断类上是否有 AsyncComponent 注解</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>AnnotationUtils<span class="token punctuation">.</span><span class="token function">isAnnotationPresent</span><span class="token punctuation">(</span>AsyncComponent<span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span> AopUtils<span class="token punctuation">.</span><span class="token function">getTargetClass</span><span class="token punctuation">(</span>component<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 提交到线程池中</span>
      executor<span class="token punctuation">.</span><span class="token function">submit</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span> component<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token keyword">return</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 同步 SpringBean 直接执行。</span>
    component<span class="token punctuation">.</span><span class="token function">invoke</span><span class="token punctuation">(</span>content<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">我们把原来的执行代码替换成使用组件执行器执行，如下图：<br>
<img class="" src="https://img.mukewang.com/5dd5fd4a0001878c16481102.png" data-original="//img.mukewang.com/5dd5fd4a0001878c16481102.png" alt="图片描述"></p>
</div><div class="cl-preview-section"><h2 id="测试" style="font-size: 30px;">6 测试</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">以上步骤完成之后，简单的流程引擎就已经完成了，我们简单地在项目启动的时候加上测试，代码如下：<br>
<img class="" src="https://img.mukewang.com/5dd5fd3d0001c5c416681156.png" data-original="//img.mukewang.com/5dd5fd3d0001c5c416681156.png" alt="图片描述"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">更严谨的做法，是会写单元测试来测试流程引擎，为了快一点，我们直接在项目启动类上加上了测试代码。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">运行之后的关键结果如下：</p>
</div><div class="cl-preview-section"><pre class="  language-java"><code class="prism  language-java"><span class="token punctuation">[</span>main<span class="token punctuation">]</span> demo<span class="token punctuation">.</span>sixth<span class="token punctuation">.</span>SynchronizedDemo<span class="token operator">:</span> SynchronizedDemo init begin
<span class="token punctuation">[</span>main<span class="token punctuation">]</span> demo<span class="token punctuation">.</span>sixth<span class="token punctuation">.</span>SynchronizedDemo<span class="token operator">:</span> SynchronizedDemo init end
<span class="token punctuation">[</span>main<span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>FlowCenter <span class="token operator">:</span> init success<span class="token punctuation">,</span>flowMap is <span class="token punctuation">{</span><span class="token string">"flow1"</span><span class="token operator">:</span><span class="token punctuation">{</span><span class="token string">"PARAM_VALID"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"AFTER_TRANSACTION"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string">"$ref"</span><span class="token operator">:</span><span class="token string">"$.flow1.PARAM_VALID[0]"</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token string">"$ref"</span><span class="token operator">:</span><span class="token string">"$.flow1.PARAM_VALID[1]"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"BUSINESS_VALID"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string">"$ref"</span><span class="token operator">:</span><span class="token string">"$.flow1.PARAM_VALID[0]"</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token string">"$ref"</span><span class="token operator">:</span><span class="token string">"$.flow1.PARAM_VALID[1]"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span><span class="token string">"IN_TRANSACTION"</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token punctuation">{</span><span class="token string">"$ref"</span><span class="token operator">:</span><span class="token string">"$.flow1.PARAM_VALID[0]"</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span><span class="token string">"$ref"</span><span class="token operator">:</span><span class="token string">"$.flow1.PARAM_VALID[1]"</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
o<span class="token punctuation">.</span>s<span class="token punctuation">.</span>j<span class="token punctuation">.</span>e<span class="token punctuation">.</span>a<span class="token punctuation">.</span>AnnotationMBeanExporter  <span class="token operator">:</span> Registering beans <span class="token keyword">for</span> JMX exposure on startup
<span class="token punctuation">[</span>main<span class="token punctuation">]</span> s<span class="token punctuation">.</span>b<span class="token punctuation">.</span>c<span class="token punctuation">.</span>e<span class="token punctuation">.</span>t<span class="token punctuation">.</span>TomcatEmbeddedServletContainer <span class="token operator">:</span> Tomcat started on <span class="token function">port</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token function">8080</span> <span class="token punctuation">(</span>http<span class="token punctuation">)</span>
<span class="token punctuation">[</span>main<span class="token punctuation">]</span> demo<span class="token punctuation">.</span>DemoApplication <span class="token operator">:</span> Started DemoApplication in <span class="token number">5.377</span> <span class="token function">seconds</span> <span class="token punctuation">(</span>JVM running <span class="token keyword">for</span> <span class="token number">6.105</span><span class="token punctuation">)</span>
<span class="token punctuation">[</span>main<span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>BeanOne <span class="token operator">:</span> BeanOne is run<span class="token punctuation">,</span>thread name is main
<span class="token punctuation">[</span>main<span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>BeanOne <span class="token operator">:</span> BeanOne is run<span class="token punctuation">,</span>thread name is main
<span class="token punctuation">[</span>pool<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>thread<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>BeanTwo <span class="token operator">:</span> BeanTwo is run<span class="token punctuation">,</span>thread name is pool<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>thread<span class="token operator">-</span><span class="token number">1</span>
<span class="token punctuation">[</span>main<span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>BeanOne <span class="token operator">:</span> BeanOne is run<span class="token punctuation">,</span>thread name is main
<span class="token punctuation">[</span>pool<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>thread<span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>BeanTwo <span class="token operator">:</span> BeanTwo is run<span class="token punctuation">,</span>thread name is pool<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>thread<span class="token operator">-</span><span class="token number">2</span>
<span class="token punctuation">[</span>pool<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>thread<span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>BeanTwo <span class="token operator">:</span> BeanTwo is run<span class="token punctuation">,</span>thread name is pool<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>thread<span class="token operator">-</span><span class="token number">3</span>
<span class="token punctuation">[</span>main<span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>BeanOne <span class="token operator">:</span> BeanOne is run<span class="token punctuation">,</span>thread name is main
<span class="token punctuation">[</span>pool<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>thread<span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">]</span> demo<span class="token punctuation">.</span>three<span class="token punctuation">.</span>flow<span class="token punctuation">.</span>BeanTwo <span class="token operator">:</span> BeanTwo is run<span class="token punctuation">,</span>thread name is pool<span class="token operator">-</span><span class="token number">1</span><span class="token operator">-</span>thread<span class="token operator">-</span><span class="token number">4</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">从运行结果中，我们可以看到 BeanTwo 已经被多个不同的线程异步执行了。</p>
</div><div class="cl-preview-section"><h2 id="总结" style="font-size: 30px;">总结</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">这是一个线程池在简单流程引擎上的运用实站，虽然这个流程引擎看起来比较简单，但在实际工作中，还是非常好用的，大家可以把代码拉下来，自己尝试一下，调试一下参数，比如当我新增 SpringBean 的时候，流程引擎的表现如何。</p>
</div>}
                        </div>
                    </div>
                                            <!-- 买过的阅读 -->
                        <div class="art-next-prev clearfix">
                                                                                                <!-- 已买且开放 或者可以试读 -->
                                    <a href="/read/47/article/881">
                                                                    <div class="prev l clearfix">
                                        <div class="icon l">
                                            <i class="imv2-arrow3_l"></i>
                                        </div>
                                        <p>
                                            39 经验总结：不同场景，如何使用线程池     
                                        </p>
                                    </div>
                                </a>
                                                                                                                            <!-- 已买且开放 或者可以试读 -->
                                    <a href="/read/47/article/883">
                                                                    <div class="next r clearfix">
                                        <p>
                                            41 突破难点：如何看 Lambda 源码  
                                        </p>
                                        <div class="icon r">
                                            <i class="imv2-arrow3_r"></i>
                                        </div>

                                    </div>
                                </a>
                                                    </div>
                                    </div>
                <div class="comments-con js-comments-con" id="coments_con">
                </div>

                
            </div>
            
            
            

        </div>
    </div>
</div>

<div class="modal modal-jiaQun-new hide" id="modal-jiaQun">
    <div class="inner" style="">
        <div class="modal-close js-close-jiaQun">
            <i class="imv2-close"></i>
        </div>
        <div class="content">
            <img src="https://img.mukewang.com/5d634d40000119e505400602.jpg">
            <div class="right-info">
                <div class="title">
                    扫码加入慕课Java核心用户群
                </div>
                <div class="desc">
                                            <p class="mb6">验证信息：<span id="joincode">1909271435058473</span><span class="copy js-copy-joincode">复制</span></p>
                                        <p class="mb6">QQ讨论群号：906691736</p>
                                            <p>QQ群URL：<a href="https://jq.qq.com/?_wv=1027&amp;k=55RtSbJ" target="_blank">点击访问</a></p>
                                    </div>
            </div>
            <p class="tip">若遇到搜索不到QQ群或加群失败，请联系客服邮箱:kf@imooc.com</p>
        </div>
    </div>
</div>
 
<!-- 专栏介绍页专栏评价 -->

<!-- 专栏介绍页底部三条评价 -->

<!-- 专栏阅读页弹层目录和介绍页页面目录 -->

<!-- 专栏阅读页发布回复 -->

<!-- 专栏阅读页发布评论 -->

<!-- 专栏阅读页底部评论 -->

<!-- 专栏阅读 单个 评论 -->

<!-- 新增回复和展开三条以外回复 -->

<!-- 立即订阅的弹窗 -->












</div></body></html>